//
//  HomeTitleView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 21.03.25.
//

import SwiftUI

struct HomeTitleView: View {
    
    private let user = KoiAccountingUserManager.standard.user()
    
    var onTrailGuideClick:()->Void
    
    @State var showTrailGuide = false
    @State var navigationToMineView = false
    @State var isTrail = false
    
    var body: some View {
        HStack {
            Color.white.opacity(0).frame(width:24,height:24)
            Spacer()
            Text("锦鲤记账")
                .foregroundColor(Color.koiColorTitle)
                .font(.system(size: 18))
                .bold()
            Spacer()
            Button(action: {
                if isTrail {
                    onTrailGuideClick()
                } else {
                    navigationToMineView = true
                }
            }) {
                AsyncImage(url:user.getHeadImg()) { phase in
                    switch phase {
                    case .empty:
                        Image("koi_default_avatar128")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 24, height: 24)
                    case .success(let image):
                        image
                            .resizable()
                            .scaledToFit()
                            .frame(width: 24, height: 24)
                            .cornerRadius(30)
                    case .failure:
                        Image("koi_default_avatar128")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 24, height: 24)
                    @unknown default:
                        Image("koi_default_avatar128")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 24, height: 24)
                    }
                }
            }
        }
        .frame(height: 44)
        .navigationDestination(isPresented: $navigationToMineView, destination: { MineView() })
        .onAppear {
            isTrail = KoiAccountingUserManager.standard.isTrail()
        }
        
    }
}

#Preview {
    HomeTitleView(onTrailGuideClick: {})
}
